package com.chenpeijian.demo.common.aspect;

import com.chenpeijian.demo.common.annotation.ModifyParamPotent;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @program: student_code
 * @description:
 * @author: 陈培建
 * @create: 2024-12-20 15:17
 **/
@Aspect
@Component
public class ModifyPotentAspect {
    @Pointcut("@annotation(com.chenpeijian.demo.common.annotation.ModifyParamPotent)")
    public void idempotentAspect() {
    }

    @Around("idempotentAspect()")
    public Object interceptor(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        ModifyParamPotent modifyRequestParam = method.getAnnotation(ModifyParamPotent.class);
        // 获取方法参数
        Object[] args = joinPoint.getArgs();

        // 假设我们只处理第一个参数为String类型的情况
        if (args.length > 0 && args[0] instanceof String) {
            // 修改参数值
            String originalValue = (String) args[0];
            String modifiedValue = modifyRequestParam.value() + "_" + originalValue;
            args[0] = modifiedValue;
        }

        // 继续执行目标方法
        return joinPoint.proceed(args);
    }
}
